import { CopyControllerBase } from "./CopyControllerBase";
import { CrossStairButton } from "../crossStair/CrossStairButton";
import { CrossStairInfoPanel } from "../crossStair/CrossStairInfoPanel";
import { CrossStairRankingPanel } from "../crossStair/CrossStairRankingPanel";
import { TCopy } from "../../../../../Message/Db/Tables/TCopy";
import { HighlightManager } from "../../../manager/HighlightManager";
import { Dispatcher } from "../../../../mvc/core/Dispatcher";
import { DataEvent } from "../../../events/DataEvent";
import { EventName } from "../../../mvc/EventName";
import { NetDispatcher } from "../../../../mvc/core/NetDispatcher";
import { EPublicCommand } from "../../../../../Message/Command/EPublicCommand";
import { MessageBlock } from "../../../../../Framework/MQ/MessageBlock";
import { SCrossStairOpen } from "../../../../../Message/Public/SCrossStairOpen";
import { ClockManager } from "../../../manager/ClockManager";
import { Language } from "../../../../../extend/language/Language";
import { Alert } from "../../../../../com/mui/controls/Alert";
import { GameProxy } from "../../../mvc/GameProxy";
import { CopyConfig } from "../../../resource/CopyConfig";
import { SCrossStairInfo } from "../../../../../Message/Public/SCrossStairInfo";
import { SCrossStairRanks } from "../../../../../Message/Public/SCrossStairRanks";
type int = number;
//class CrossStairController
    
    export  class CrossStairController extends CopyControllerBase
    {
        constructor()
        {
            super();
            return;
        }

        public get crossStairBtn(): CrossStairButton
        {
            if (!this._crossStairBtn) 
            {
                this._crossStairBtn = new CrossStairButton();
                this._crossStairBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onCrossStairBtnClick);
            }
            return this._crossStairBtn;
        }

        public get infoPanel(): CrossStairInfoPanel
        {
            if (!this._infoPanel) 
            {
                this._infoPanel = new CrossStairInfoPanel();
            }
            return this._infoPanel;
        }

        public get rankingPanel(): CrossStairRankingPanel
        {
            if (!this._rankingPanel) 
            {
                this._rankingPanel = new CrossStairRankingPanel();
            }
            return this._rankingPanel;
        }

        public /* override */ enterCopy(arg1: TCopy): void
        {
            super.enterCopy(arg1);
            this.infoPanel.show();
            this.infoPanel.updateData(this._stairInfo);
            this.infoPanel.updateTime(this.getLeftTime());
            this.infoPanel.updatePosition();
            if (this._crossStairBtn) 
            {
                HighlightManager.instance.glowEffectButton(this._crossStairBtn, false);
            }
            Dispatcher.dispatchEvent(new DataEvent(EventName.ShowGameMainUI, false));
            return;
        }

        public /* override */ leaveCopy(): void
        {
            super.leaveCopy();
            if (this._infoPanel) 
            {
                this._infoPanel.hide();
            }
            if (this._rankingPanel) 
            {
                this._rankingPanel.hide();
            }
            Dispatcher.dispatchEvent(new DataEvent(EventName.ShowGameMainUI, true));
            return;
        }

        protected /* override */ initServer(): void
        {
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicCrossStairOpen, this.onCrossStairOpen);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicCrossStairClose, this.onCrossStairClose);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicCrossStairInfo, this.onCrossStairInfo);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicCrossStairRank, this.onCrossStairRank);
            return;
        }

        protected /* override */ addListener(): void
        {
            Dispatcher.addEventListener(EventName.CopyShowCrossStairRanking, this.onShowRankingWindow);
            return;
        }

        protected /* override */ removeListener(): void
        {
            Dispatcher.removeEventListener(EventName.CopyShowCrossStairRanking, this.onShowRankingWindow);
            return;
        }

        /* internal  */onCrossStairOpen(arg1: MessageBlock): void
        {
            this.crossStairBtn.show();
            if (!copy) 
            {
                HighlightManager.instance.glowEffectButton(this.crossStairBtn, true);
            }
            this._open = arg1.messageBase as SCrossStairOpen;
            var loc1=this.getLeftTime();
            this.crossStairBtn.updateTime(loc1);
            if (this._infoPanel && !this._infoPanel.isHide) 
            {
                this._infoPanel.updateTime(loc1);
            }
            return;
        }

        /* internal  */getLeftTime(): int
        {
            if (!this._open) 
            {
                return 0;
            }
            var loc1=ClockManager.instance.nowDate.time > this._open.openDt.time ? ClockManager.instance.nowDate : this._open.openDt;
            return (this._open.endDt.time - loc1.time) / 1000;
        }

        /* internal  */onCrossStairClose(arg1: MessageBlock): void
        {
            if (this._crossStairBtn) 
            {
                this._crossStairBtn.hide();
                HighlightManager.instance.glowEffectButton(this._crossStairBtn, false);
            }
            return;
        }

        /* internal  */onCrossStairBtnClick(arg1: Object /* flash.events.MouseEvent */): void
        {
            var loc1=null;
            if (!copy) 
            {
                loc1 = Language.getString(60825);
                Alert.show(loc1, null, Alert.YES | Alert.NO, null, this.onAlertCloseHandler);
            }
            return;
        }

        /* internal  */onAlertCloseHandler(arg1: int): void
        {
            if (arg1 == Alert.YES) 
            {
                GameProxy.copy.enterCrossStair();
            }
            return;
        }

        /* internal  */onCrossStairInfo(arg1: MessageBlock): void
        {
            var loc1=CopyConfig.instance.getCrossStairTopLayer();
            var loc2=arg1.messageBase as SCrossStairInfo;
            if (loc2.floor >= loc1 && (!this._stairInfo || this._stairInfo.floor < loc2.floor)) 
            {
                flash.utils.setTimeout(this.showLeaveTipAlert, 2000);
            }
            this._stairInfo = loc2;
            if (this._infoPanel) 
            {
                this._infoPanel.updateData(this._stairInfo);
            }
            return;
        }

        /* internal  */showLeaveTipAlert(): void
        {
            if (copy) 
            {
                Alert.buttonWidth = 75;
                Alert.okLabel = Language.getString(60837);
                Alert.calcelLabel = Language.getString(60838);
                Alert.show(Language.getString(60836), Language.getString(69900), Alert.OK | Alert.CANCEL, null, this.onLeaveTipAlertCloseHandler);
            }
            return;
        }

        /* internal  */onLeaveTipAlertCloseHandler(arg1: int): void
        {
            if (arg1 == Alert.OK) 
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.CopyLeaveWithoutComfirm));
            }
            return;
        }

        /* internal  */onShowRankingWindow(arg1: DataEvent): void
        {
            if (this._rankingPanel && !this._rankingPanel.isHide) 
            {
                this._rankingPanel.hide();
            }
            else 
            {
                this.rankingPanel.show();
                if (ClockManager.instance.nowDate.time - this._flagtime > 1000) 
                {
                    this._flagtime = ClockManager.instance.nowDate.time;
                    this.rankingPanel.getRank();
                }
            }
            return;
        }

        /* internal  */onCrossStairRank(arg1: MessageBlock): void
        {
            if (this._rankingPanel && !this._rankingPanel.isHide) 
            {
                this._rankingPanel.updateRankingData(arg1.messageBase as SCrossStairRanks);
            }
            return;
        }

        /* internal */ /* var */_crossStairBtn: CrossStairButton;

        /* internal */ /* var */_infoPanel: CrossStairInfoPanel;

        /* internal */ /* var */_rankingPanel: CrossStairRankingPanel;

        /* internal */ /* var */_open: SCrossStairOpen;

        /* internal */ /* var */_stairInfo: SCrossStairInfo;

        /* internal */ /* var */_flagtime: int;
    }
